package com.app;

import java.util.regex.Pattern;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Spinner;
import android.widget.Toast;

public class GraphicsActivity extends Activity implements
		OnItemSelectedListener {

	private GraphEdit edit1;
	private GraphEdit edit2;
	private GraphView graphView;
	private AdView adView;
	private String[] types = { "y=f(x)", "x=f(t),y=f(t)", "r=q(a)", "x+y=0" };
	private Display display;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		graphView = (GraphView) findViewById(R.id.graphView1);
		graphView.setActivity(this);
		final ImageView hendler = (ImageView) findViewById(R.id.image);
		final SlidingDrawer sd = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
		display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay();

		// fullscreen
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// ad
		adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());

		// set spinner
		Spinner spinner = (Spinner) findViewById(R.id.spinner1);
		spinner.setOnItemSelectedListener(this);
		ArrayAdapter<String> list = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, types);
		list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(list);

		// edits
		edit1 = (GraphEdit) findViewById(R.id.editText1);
		edit2 = (GraphEdit) findViewById(R.id.editText2);

		// draw button
		Button drawbutton = (Button) findViewById(R.id.button1);
		drawbutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				buildGraph();
				sd.animateClose();

			}
		});

		// slider listeners
		sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {

			@Override
			public void onDrawerOpened() {
				edit1.requestFocus();
				hendler.setImageResource(R.drawable.adown);
			}
		});

		sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {

			@Override
			public void onDrawerClosed() {
				buildGraph();
				hendler.setImageResource(R.drawable.aup);
			}
		});

		Keyboard keyboard = new Keyboard(this, R.layout.key);
		final KeyboardView keyboardView = (KeyboardView) findViewById(R.id.myKeyboardViewXML);
		keyboardView.setKeyboard(keyboard);
		keyboardView.setVisibility(View.VISIBLE);
		keyboardView.setOnKeyboardActionListener(new KeyboardListener(this));
		
	}

	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;

	}

	public void onContentChanged() {
		super.onContentChanged();
		graphView = (GraphView) findViewById(R.id.graphView1);
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		EditText edit2 = (EditText) findViewById(R.id.editText2);

		switch (position) {
		case 1:
			edit2.setEnabled(true);
			edit2.setVisibility(View.VISIBLE);
			break;
		default:
			edit2.setEnabled(false);
			edit2.setVisibility(View.GONE);
			break;
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	public void onDestroy() {
		adView.destroy();
		super.onDestroy();
	}
	

	private void buildGraph() {

		String[] functions = new String[2];
		functions[0] = edit1.getText().toString().trim();

		if (edit2.isEnabled()) {
			functions[1] = edit2.getText().toString().trim();
		}

		graphView.setFunction(functions);
	}

}
